package com.zzx.bms.service.impl;

import com.zzx.bms.domain.*;
import com.zzx.bms.persistence.BookMapper;
import com.zzx.bms.persistence.BorrowMapper;
import com.zzx.bms.persistence.JournalMapper;
import com.zzx.bms.persistence.UserMapper;
import com.zzx.bms.service.IBorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * Created by SamZhao on 2016/12/28.
 */
@Service(value="borrowService")
public class BorrowServiceImpl implements IBorrowService {

    @Autowired
    private BorrowMapper borrowMapper;

    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private JournalMapper journalMapper;

    @Autowired
    private UserMapper userMapper;


    @Override
    public int checkBorrowOut(String bookId, String userId, String userNumber, String bookType) {
        User user = userMapper.selectByPrimaryKey(Integer.parseInt(userId));
        int flag = 0;
        if(bookType == "0"){
            Book book = bookMapper.selectByPrimaryKey(Integer.parseInt(bookId));
            if(book.getBorrowNum()>=book.getTotalNum())
                flag = 1;
        }
        if(bookType == "1"){
            Journal book = journalMapper.selectByPrimaryKey(Integer.parseInt(bookId));
            if(book.getBorrowNum()>=book.getTotalNum())
                flag = 1;
        }
        if(flag == 0 ){
            BorrowExample exp = new BorrowExample();
            BorrowExample.Criteria crt = exp.createCriteria();
            crt.andBookIdEqualTo(Integer.parseInt(bookId));
            crt.andUserIdEqualTo(Integer.parseInt(userId));
            crt.andStatusEqualTo("0");
            crt.andBorrowTypeEqualTo(bookType);
            flag = borrowMapper.countByExample(exp);
            if(flag > 0)
                flag = 2;
            else
                flag = 0;
        }

        return flag;
    }

    @Override
    public int checkBorrowBack(String bookId, String userId, String userNumber, String bookType) {
        int flag = 0;
        BorrowExample exp = new BorrowExample();
        BorrowExample.Criteria crt = exp.createCriteria();
        crt.andBookIdEqualTo(Integer.parseInt(bookId));
        crt.andUserIdEqualTo(Integer.parseInt(userId));
        crt.andStatusEqualTo("0");
        crt.andBorrowTypeEqualTo(bookType);
        flag = borrowMapper.countByExample(exp);
        if(flag > 0)
            flag = 0;
        else
            flag = 1;
        return flag;
    }

    @Override
    public int saveBorrowOut(String bookId, String userId, String userNumber, String bookType) {
        if("0".equals(bookType.trim())){
            Book book = bookMapper.selectByPrimaryKey(Integer.parseInt(bookId));
            if(book.getTotalNum()>book.getBorrowNum()){
                Borrow borrow = new Borrow();
                borrow.setBookId(Integer.parseInt(bookId));
                borrow.setBorrowType(bookType);
                borrow.setUserId(Integer.parseInt(userId));
                borrow.setBorrowOut(new Date());
                borrow.setStatus("0");
                Date date= new Date();//取时间
                Calendar calendar   =   new GregorianCalendar();
                calendar.setTime(date);
                calendar.add(calendar.DATE,15);//把日期往后增加15天.整数往后推,负数往前移动
                date=calendar.getTime();   //这个时间就是日期往后推15天的结果
                borrow.setBorrowRequire(date);
                int flag = borrowMapper.insert(borrow);
                book.setBorrowNum(book.getBorrowNum()+1);
                int flag1 = bookMapper.updateByPrimaryKeySelective(book);
                if(flag ==1 && flag1 ==1)
                    return 1;
                else return 0;
            }else
                return 0;
        }
        if("1".equals(bookType.trim())){
            Journal book = journalMapper.selectByPrimaryKey(Integer.parseInt(bookId));
            if(book.getTotalNum()>book.getBorrowNum()){
                Borrow borrow = new Borrow();
                borrow.setBookId(Integer.parseInt(bookId));
                borrow.setBorrowType(bookType);
                borrow.setUserId(Integer.parseInt(userId));
                borrow.setBorrowOut(new Date());
                borrow.setStatus("0");
                Date date= new Date();//取时间
                Calendar calendar   =   new GregorianCalendar();
                calendar.setTime(date);
                calendar.add(calendar.DATE,15);//把日期往后增加15天.整数往后推,负数往前移动
                date=calendar.getTime();   //这个时间就是日期往后推15天的结果
                borrow.setBorrowRequire(date);
                int flag = borrowMapper.insert(borrow);
                book.setBorrowNum(book.getBorrowNum()+1);
                int flag1 = journalMapper.updateByPrimaryKeySelective(book);
                if(flag ==1 && flag1 ==1)
                    return 1;
                else return 0;
            }else
                return 0;
        }
        return 0;
    }

    @Override
    public int saveBorrowBack(String bookId, String userId, String userNumber, String bookType) {
        int flag = 0;
        BorrowExample exp = new BorrowExample();
        BorrowExample.Criteria crt = exp.createCriteria();
        crt.andBookIdEqualTo(Integer.parseInt(bookId));
        crt.andUserIdEqualTo(Integer.parseInt(userId));
        crt.andStatusEqualTo("0");
        crt.andBorrowTypeEqualTo(bookType);
        List<Borrow> list = borrowMapper.selectByExample(exp);
        if(list !=null && list.size()>0){
            Borrow temp = list.get(0);
            temp.setBorrowBack(new Date());
            temp.setStatus("1");
            flag = borrowMapper.updateByPrimaryKeySelective(temp);
            int flag1 = 0;
            if("1".equals(bookType.trim())){
                Journal book = journalMapper.selectByPrimaryKey(Integer.parseInt(bookId));
                book.setBorrowNum(book.getBorrowNum()-1);
                flag1 = journalMapper.updateByPrimaryKeySelective(book);
            }
            if("0".equals(bookType.trim())){
                Book book = bookMapper.selectByPrimaryKey(Integer.parseInt(bookId));
                book.setBorrowNum(book.getBorrowNum()-1);
                flag1 = bookMapper.updateByPrimaryKeySelective(book);
            }

            if(flag ==1 && flag1 ==1)
                return 1;
            else return 0;
        }
        return flag;
    }

    @Override
    public List<Borrow> findAllBorrowList() {
        BorrowExample exp = new BorrowExample();
        BorrowExample.Criteria crt = exp.createCriteria();
        List<Borrow> list = borrowMapper.selectByExample(exp);
        for(Borrow temp : list){
            if(temp.getBookId()!=null){
                if("1".equals(temp.getBorrowType().trim())){
                    Journal book = journalMapper.selectByPrimaryKey(temp.getBookId());
                    temp.setBookName(book.getName());
                }
                if("0".equals(temp.getBorrowType().trim())){
                    Book book = bookMapper.selectByPrimaryKey(temp.getBookId());
                    temp.setBookName(book.getName());
                }
            }
            if(temp.getUserId()!=null){
                User user = userMapper.selectByPrimaryKey(temp.getUserId());
                temp.setUserName(user.getRealname());
            }
        }
        return list;
    }

    @Override
    public int getBorrowTotal() {
        BorrowExample exp = new BorrowExample();
        BorrowExample.Criteria crt = exp.createCriteria();
        crt.andStatusEqualTo("0");
        return borrowMapper.countByExample(exp);
    }
}
